package library;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
/*
public class Template {
	protected Hashtable<String,Object> _variables = new Hashtable<String, Object>();
	protected Controller _controller;
	protected String _action;
	
	public Template(final Controller ctrl, final String action) {
		this._controller = ctrl;
		this._action = action;
	}
	
	public void set(String key, Object value){
		this._variables.put(key, value);
	}
	
	public String render(){
		String ctrl = this._controller.getClass().getName();
		return "application/views/"+ctrl+"/"+_action+".jsp";
	}
	
	public void renderpartial(HttpServletRequest opt, String action, String params[]) throws Throwable{
		String str="";
		Method cmd;
		Class<?> parameterTypes = null;
		
		cmd = this._controller.getClass().getMethod(_action, parameterTypes);
		cmd.invoke(_controller, (Object) params);
		
		cmd = this._controller.getClass().getMethod(_action, parameterTypes);
		cmd.invoke(_controller, (Object) params);

		cmd = this._controller.getClass().getMethod("afteraction", parameterTypes);
		cmd.invoke(_controller, (Object) params);
		
		Iterator<String> it = this._variables.keySet().iterator();
		while(it.hasNext()) {
			str = it.next();
			opt.setAttribute(str, this._variables.get(str));
		}
	}
}
*/
import javax.servlet.http.HttpServletResponse;

public class Template{
	public HttpServlet _controller;
	public String _action;
	
	public Template(HttpServlet ctrl) {
		_controller = ctrl;
	}
	
	/**
	 * @see HttpServlet#foward(HttpServletRequest request, HttpServletResponse response, String page)
	 */
	public void forward(HttpServletRequest request, HttpServletResponse response, String page) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ServletContext sc = _controller.getServletContext();
		RequestDispatcher rd = sc.getRequestDispatcher(page);
		rd.forward(request,response);
		
	}
	
	public void renderPage(HttpServletRequest request, HttpServletResponse response, String action) throws Throwable{
		//invoke action
		if(action == null) {
			response.sendRedirect("administrator?op=login");
		} else {
			_action = action;
			request.setAttribute("Template", this);
			forward(request, response, "/layout/layout.jsp");
		}
	}
}